const puppeteer = require('puppeteer');
const axios = require('axios');
const fs = require('fs');
const uuid = require('node-uuid');
var path = require('path');
var downimage = require('./downimage.js');
var havedown = 0;
(async() => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    fs.exists('./html/'+new Date().toLocaleDateString(),exists=>{
        if  (!exists)
        {
            fs.mkdirSync(`./html/${new Date().toLocaleDateString()}`)
        }
    })
    fs.exists('./html/'+new Date().toLocaleDateString()+'/images/',exists=>{
        if  (!exists)
        {
            fs.mkdirSync(`./html/${new Date().toLocaleDateString()}/images/`)
        }
    })
   
    await page.goto('http://www.playno1.com/forbid.htm');
    await page.click('body > div > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > div > button:nth-child(1) > span')
    await page.waitForSelector('#portal_block_4255_content > div > ul > li:nth-child(1) > a')
    await page.click('#portal_block_4255_content > div > ul > li:nth-child(1) > a')
    await page.waitForSelector('#ct > div.mn > div:nth-child(4) > div > a.last')
    let imgURL = await page.evaluate(() => {
        let imgURL = []
        let selector = '.fire_imgbox > a';
        let imgUrlList = [...document.querySelectorAll(selector)];
        imgUrlList.forEach(e => {
            imgURL.push(e.href)
        })
        return imgURL
    });
    console.log('有'+imgURL.length+'条新闻')
    for(i=0;i<=imgURL.length;i++)
    {
        await page.goto(imgURL[i],{timeout:0})
        console.log(imgURL[i]+'跳转成功')    
        await page.waitFor(1*1000)
        
        
        await page.screenshot({
            path: 'example'+i+'.png'
        });
        await craw(page,imgURL[i]);
    }
    await browser.close();
})();

async function craw(page,e) {
   
    let pagetitle=await page.$eval('#ct > div.mn > div.bm.vw > div.h.hm > h1', (element) => {
        return element.innerHTML
      })
      fs.exists(`./html/${new Date().toLocaleDateString()}/${pagetitle}.html`,exists=>{
        if (exists)
        {
            console.log(pagetitle+'已经下载过了')
            havedown++
            if (havedown>10)
            {
                process.exit(0)
            }
           
        }
    })
    let imgurls=await downimage(page,e)
    let pagecontent=await page.$eval('#ct > div.mn > div.bm.vw', (element) => {
        return element.innerHTML
      })
    imgurls.remoteUrl.forEach((e,i)=>{
        if (e!=null)
        {
           
            if (pagecontent.indexOf(e)>0)
            {
                do 
                {
                    pagecontent=pagecontent.replace(e,'./images/'+imgurls.localUrl[i])
                }
                while (pagecontent.indexOf(e)>0)
               
            }
            else{
                console.log(e+'~~~未找到')
            }
            
        }
    })
    pagecontent='<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'+pagecontent;
  
    fs.writeFile(`./html/${new Date().toLocaleDateString()}/${pagetitle}.html`,pagecontent,err=>{
        if(err) console.log('写文件操作失败');
        else console.log('写文件操作成功');
    })
    
    
}